﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYElectricSetting;

namespace HYRElectric.WirePlace
{
	public partial class DlgCreateConduitConfig : System.Windows.Forms.Form
	{
		public DlgCreateConduitConfig(Document doc)
		{
			this.InitializeComponent();
			this.m_doc = doc;
		}

		public string getStringInstallType()
		{
			return this.m_sInstallType;
		}

		public string getStringConduitType()
		{
			return this.m_ConduitType;
		}

		public InstallType getInstallType()
		{
			return this.m_installType;
		}

		public bool getCreateType()
		{
			return this.m_bIsUser;
		}

		public double getDiameter()
		{
			return this.m_dDiameter;
		}

		public ElementId getConduitTypeID()
		{
			return this.m_ConduitTypeID;
		}

		private void WireParamNon_modeForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeConduitInfo();
			try
			{
				this.xmlHelper.LoadXml(this.path, "Root");
			}
			catch (Exception)
			{
				File.Delete(this.path);
			}
			try
			{
				this.ConduitTypeCombox.Items.Clear();
				foreach (RevitConduitType item in this.revitConduitTypes)
				{
					this.ConduitTypeCombox.Items.Add(item);
				}
				this.ConduitTypeCombox.SelectedIndex = 0;
				RevitConduitType revitConduitType = this.ConduitTypeCombox.SelectedItem as RevitConduitType;
				this.InitDiameterCombox(revitConduitType);
				this.LoadDlgData();
			}
			catch
			{
			}
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCreateConduitConfig", "Params"));
			string attribute = this.xmlHelper.GetAttribute("ConduitTypeID");
			if (!string.IsNullOrEmpty(attribute))
			{
				foreach (object obj in this.ConduitTypeCombox.Items)
				{
					if ((obj as RevitConduitType).ConduitTypeId.ToString() == attribute)
					{
						this.ConduitTypeCombox.SelectedItem = obj;
						break;
					}
				}
			}
			if (this.ConduitTypeCombox.Items.Count > 0)
			{
				string attribute2 = this.xmlHelper.GetAttribute("ConduitDiameter");
				if (!string.IsNullOrEmpty(attribute2))
				{
					this.PipeDiameterCombox.Text = attribute2;
				}
			}
			if (this.xmlHelper.GetAttribute("DataSource").Equals("user"))
			{
				this.m_bIsUser = true;
				this.RbCreateByUser.Checked = true;
			}
			else
			{
				this.m_bIsUser = false;
				this.RbCreateByData.Checked = true;
			}
			this.m_sInstallType = this.xmlHelper.GetAttribute("InstallType");
			if (this.m_sInstallType.Equals("CC"))
			{
				this.m_installType = InstallType.CC;
				this.RbCC.Checked = true;
			}
			else if (this.m_sInstallType.Equals("CE"))
			{
				this.m_installType = InstallType.CE;
				this.RbCE.Checked = true;
			}
			else if (this.m_sInstallType.Equals("SCE"))
			{
				this.RbSCE.Checked = true;
				this.m_installType = InstallType.SCE;
				this.btnTopHighSet.Enabled = true;
			}
			else if (this.m_sInstallType.Equals("WC"))
			{
				this.m_installType = InstallType.WC;
				this.RbWC.Checked = true;
			}
			else if (this.m_sInstallType.Equals("WS"))
			{
				this.RbWS.Checked = true;
				this.m_installType = InstallType.WS;
			}
			else if (this.m_sInstallType.Equals("FC"))
			{
				this.RbFC.Checked = true;
				this.m_installType = InstallType.FC;
				this.btnGroundHighSet.Enabled = true;
			}
			else
			{
				this.m_installType = InstallType.CC;
				this.RbCC.Checked = true;
			}
			string attribute3 = this.xmlHelper.GetAttribute("X");
			string attribute4 = this.xmlHelper.GetAttribute("Y");
			if (!string.IsNullOrEmpty(attribute3) && !string.IsNullOrEmpty(attribute4))
			{
				base.Location = new System.Drawing.Point(Convert.ToInt32(attribute3), Convert.ToInt32(attribute4));
			}
		}

		private void InitializeConduitInfo()
		{
			this.revitConduitTypes = ElectricSettingDataAssist.GetRevitConduitTypes(this.m_doc);
			if (this.revitConduitTypes.Count == 0)
			{
				throw new Exception("请先进行线管设置。");
			}
		}

		private void UpdateConduitDiameter()
		{
			if (this.ConduitTypeCombox.Items.Count > 0)
			{
				if (this.PipeDiameterCombox.Items.Count > 0)
				{
					this.PipeDiameterCombox.SelectedIndex = 0;
					this.m_dDiameter = Convert.ToDouble(this.PipeDiameterCombox.SelectedItem.ToString());
					return;
				}
				this.PipeDiameterCombox.Text = "16";
				this.m_dDiameter = 16.0;
			}
		}

		private void RbCreateByData_Click(object sender, EventArgs e)
		{
			if (this.RbCreateByUser.Checked)
			{
				this.m_bIsUser = true;
			}
			if (this.RbCreateByData.Checked)
			{
				this.m_bIsUser = false;
			}
		}

		private void RbCC_CheckedChanged(object sender, EventArgs e)
		{
			this.btnTopHighSet.Enabled = false;
			this.btnGroundHighSet.Enabled = false;
			if (this.RbCC.Checked)
			{
				this.m_installType = InstallType.CC;
				this.m_sInstallType = "CC";
				return;
			}
			if (this.RbCE.Checked)
			{
				this.m_installType = InstallType.CE;
				this.m_sInstallType = "CE";
				return;
			}
			if (this.RbSCE.Checked)
			{
				this.m_installType = InstallType.SCE;
				this.m_sInstallType = "SCE";
				this.btnTopHighSet.Enabled = true;
				return;
			}
			if (this.RbWC.Checked)
			{
				this.m_installType = InstallType.WC;
				this.m_sInstallType = "WC";
				return;
			}
			if (this.RbWS.Checked)
			{
				this.m_installType = InstallType.WS;
				this.m_sInstallType = "WS";
				return;
			}
			if (this.RbFC.Checked)
			{
				this.m_installType = InstallType.FC;
				this.m_sInstallType = "FC";
				this.btnGroundHighSet.Enabled = true;
			}
		}

		private void ConduitTypeCombox_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_ConduitType = this.ConduitTypeCombox.SelectedItem.ToString();
			RevitConduitType revitConduitType = this.ConduitTypeCombox.SelectedItem as RevitConduitType;
			this.m_ConduitTypeID = revitConduitType.ConduitTypeId;
			this.InitDiameterCombox(revitConduitType);
		}

		private void InitDiameterCombox(RevitConduitType revitConduitType)
		{
			List<int> conduitSizeSet = revitConduitType.ConduitSizeSet;
			this.PipeDiameterCombox.Items.Clear();
			if (conduitSizeSet.Count > 0)
			{
				foreach (int num in conduitSizeSet)
				{
					this.PipeDiameterCombox.Items.Add(num);
				}
				this.PipeDiameterCombox.SelectedIndex = 0;
			}
			else
			{
				this.PipeDiameterCombox.Text = "16";
			}
			this.UpdateConduitDiameter();
		}

		private void PipeDiameterCombox_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_dDiameter = Convert.ToDouble(this.PipeDiameterCombox.SelectedItem.ToString());
		}

		public void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCreateConduitConfig"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYCreateConduitConfig", "Params"));
			this.xmlHelper.SetAttribute("ConduitTypeID", ((RevitConduitType)this.ConduitTypeCombox.SelectedItem).ConduitTypeId.ToString());
			this.xmlHelper.SetAttribute("ConduitDiameter", this.PipeDiameterCombox.Text);
			string value;
			if (this.m_bIsUser)
			{
				value = "user";
			}
			else
			{
				value = "data";
			}
			this.xmlHelper.SetAttribute("DataSource", value);
			this.xmlHelper.SetAttribute("InstallType", this.m_sInstallType);
			this.xmlHelper.SetAttribute("X", base.Location.X.ToString());
			this.xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			this.xmlHelper.SaveXml("");
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void DlgCreateConduitConfig_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgCreateConduitConfig.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgCreateConduitConfig.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void btnNameSet_Click(object sender, EventArgs e)
		{
			new DlgSetRelationMap().ShowDialog();
		}

		private void btnTopHighSet_Click(object sender, EventArgs e)
		{
			new DlgSetSCEHigh().ShowDialog();
		}

		private void btnGroundHighSet_Click(object sender, EventArgs e)
		{
			new DlgSetFCHigh().ShowDialog();
		}

		private void ConduitTypeCombox_MouseHover(object sender, EventArgs e)
		{
			string text = this.ConduitTypeCombox.Text;
			this.toolTip1.SetToolTip(this.ConduitTypeCombox, text);
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CreateConduitConfig.xml");

		private const string Root = "Root";

		private const string Node = "HYCreateConduitConfig";

		private const string ChildNode = "Params";

		private const string ConduitDiameter = "ConduitDiameter";

		private const string ConduitTypeID = "ConduitTypeID";

		private const string DataSource = "DataSource";

		private const string sInstallType = "InstallType";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private bool m_bIsUser;

		private InstallType m_installType;

		private double m_dDiameter;

		private ElementId m_ConduitTypeID;

		private string m_ConduitType;

		private string m_sInstallType;

		private Document m_doc;

		private List<RevitConduitType> revitConduitTypes = new List<RevitConduitType>();
	}
}
